package com.youzhi.backend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youzhi.backend.dto.Result;
import com.youzhi.backend.entity.Role;
import com.youzhi.backend.request.*;
import com.youzhi.backend.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author xumeng
 * @since 2022-11-18
 */
@RestController
@Api(tags = "角色")
@RequestMapping("/role")
public class RoleController extends BaseController {


    @Autowired
    private IRoleService service;


    @ApiOperation("角色列表")
    @PostMapping("/page")
    public Result rolePage(@RequestBody RolePageRequest request) {
        IPage<Role> pages = service.pageRequest(request);
        return Result.ok(pages);
    }


    @RequiresPermissions("role:add")
    @ApiOperation("添加角色")
    @PostMapping("/add")
    public Result add(@RequestBody AddRoleRequest request) {
        verifyIfBlank(request, AddRoleRequest.Fields.name);
        service.addRole(request);
        return Result.ok();
    }


    @RequiresPermissions("role:edit")
    @ApiOperation("更新角色信息")
    @PostMapping("/update")
    public Result update(@RequestBody UpdateRoleRequest request) {
        verifyIfBlank(request, UpdateRoleRequest.Fields.id);
        service.updateRole(request);
        return Result.ok();
    }


    @RequiresPermissions("role:delete")
    @ApiOperation("删除角色信息")
    @PostMapping("/delete")
    public Result delete(@RequestBody DeleteRoleRequest request) {
        verifyListIsEmpty(request.getRoleIds());
        service.deleteRoles(request.getRoleIds());
        return Result.ok();
    }


    @RequiresPermissions("role:assignPrivilege")
    @ApiOperation("添加权限")
    @PostMapping("/add/privilege")
    public Result addPrivilege(@RequestBody AddPrivilegeRequest request) {
        verifyIfBlank(request, AddPrivilegeRequest.Fields.roleId);
        service.addPrivilege(request);
        return Result.ok();
    }



}

